﻿// -------------------------------------------------------------------------
//    @FileName         :    NFPlayerStatus.cpp
//    @Author           :    gaoyi
//    @Date             :    23-11-17
//    @Email			:    445267987@qq.com
//    @Module           :    NFPlayerStatus
//
// -------------------------------------------------------------------------

#include "NFCacheStatus.h"

#include "NFLogicCommon.h"
#include "NFComm/NFCore/NFServerTime.h"

NFCacheStatus::NFCacheStatus()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFCacheStatus::~NFCacheStatus()
{
}

int NFCacheStatus::CreateInit()
{
    m_cacheStatus = CACHE_STATUS_NONE;
    m_lastCreateTime = NF_ADJUST_TIMENOW();
    m_lastDisconnectTime = 0;
    m_lastLogoutTime = 0;
    m_isDisconnect = false;
    m_timeMulti = 1;
    return 0;
}

int NFCacheStatus::ResumeInit()
{
    return 0;
}

int NFCacheStatus::Tick()
{
    switch (m_cacheStatus)
    {
        case CACHE_STATUS_NONE:
        {
            if ((uint64_t)NF_ADJUST_TIMENOW() - m_lastCreateTime < PLAYER_MAX_DISCONNECT_RECONNECT_TIME * m_timeMulti)
                break;

            if (!IsCanLogout())
            {
                break;
            }

            SetPlayerStatus(CACHE_STATUS_LOGOUT);
            SetLastLogoutTime(NF_ADJUST_TIMENOW());
            NFLogInfo(NF_LOG_DEFAULT, StatusId(), "StatusId:{} status:CACHE_STATUS_NONE change to CACHE_STATUS_LOGOUT", StatusId());
        }
        break;
        case CACHE_STATUS_ONLINE:
        {
            DoOnline();
        }
        break;
        case CACHE_STATUS_OFFLINE:
        {
            if (NF_ADJUST_TIMENOW() - GetLastDiconnectTime() < PLAYER_MAX_DISCONNECT_RECONNECT_TIME * m_timeMulti * 2)
                break;

            if (!IsCanLogout())
            {
                break;
            }

            SetPlayerStatus(CACHE_STATUS_LOGOUT);
            SetLastLogoutTime(NF_ADJUST_TIMENOW());
            NFLogInfo(NF_LOG_DEFAULT, StatusId(), "StatusId:{} status:CACHE_STATUS_OFFLINE change to CACHE_STATUS_LOGOUT", StatusId());
        }
        break;
        case CACHE_STATUS_LOGOUT:
        default:
        {
            if (!IsCanDelete())
            {
                break;
            }

            DoLogout();
        }
        break;
    }
    return 0;
}

bool NFCacheStatus::IsCanLogout()
{
    return true;
}

int NFCacheStatus::DoOnline()
{
    return 0;
}

bool NFCacheStatus::IsCanDelete()
{
    return true;
}

int NFCacheStatus::DoLogout()
{
    return 0;
}

enCacheStatus NFCacheStatus::GetPlayerStatus() const
{
    return m_cacheStatus;
}

void NFCacheStatus::SetPlayerStatus(enCacheStatus status)
{
    m_cacheStatus = status;
}

uint64_t NFCacheStatus::GetCreateTime() const
{
    return m_lastCreateTime;
}

void NFCacheStatus::SetCreateTime(uint64_t createTime)
{
    m_lastCreateTime = createTime;
}

uint64_t NFCacheStatus::GetLastDiconnectTime() const
{
    return m_lastDisconnectTime;
}

void NFCacheStatus::SetLastDiconnectTime(uint64_t lastDiconnectTime)
{
    m_lastDisconnectTime = lastDiconnectTime;
}

uint64_t NFCacheStatus::GetLastLogoutTime() const
{
    return m_lastLogoutTime;
}

void NFCacheStatus::SetLastLogoutTime(uint64_t lastLogoutTime)
{
    m_lastLogoutTime = lastLogoutTime;
}

bool NFCacheStatus::IsDisconnect() const
{
    return m_isDisconnect;
}

void NFCacheStatus::SetIsDisconnect(bool isDisconnect)
{
    m_isDisconnect = isDisconnect;
}

bool NFCacheStatus::IsOnline() const
{
    return m_cacheStatus == CACHE_STATUS_ONLINE;
}

void NFCacheStatus::SetIsOnline(bool isOnline)
{
    m_cacheStatus = CACHE_STATUS_ONLINE;
}
